package com.dpwgc.provider.service;

import com.dpwgc.provider.mapper.DnMapper;
import com.dpwgc.provider.mapper.HunterMapper;
import com.dpwgc.provider.mapper.UserMapper;
import com.dpwgc.provider.model.Dn;
import com.dpwgc.provider.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
public class DnService {

    @Autowired
    DnMapper dnMapper;

    @Autowired
    HunterMapper hunterMapper;

    @Autowired
    UserMapper userMapper;

    /**
     * 创建代拿订单
     * @param dn 代拿订单信息
     * @return
     */
    public ResultUtil<Object> createDnService(Dn dn){
        ResultUtil<Object> resultUtil = new ResultUtil<>();
        try {
            int res = dnMapper.createDn(dn);

            resultUtil.setCode(200);
            resultUtil.setMsg("创建订单成功");
            resultUtil.setData(res);
            return resultUtil;
        }catch (Exception e){
            resultUtil.setCode(100);
            resultUtil.setMsg("创建订单失败");
            resultUtil.setData(e);
            return resultUtil;
        }
    }

    /**
     * 根据uuid删除代拿订单
     * @param dnUuid 代拿订单uuid
     * @return
     */
    public ResultUtil<Object> deleDnService(String dnUuid){
        ResultUtil<Object> resultUtil = new ResultUtil<>();
        try {
            int res = dnMapper.deleDn(dnUuid);

            resultUtil.setCode(200);
            resultUtil.setMsg("删除订单成功");
            resultUtil.setData(res);
            return resultUtil;
        }catch (Exception e){
            resultUtil.setCode(100);
            resultUtil.setMsg("删除订单失败");
            resultUtil.setData(e);
            return resultUtil;
        }
    }

    /**
     * 根据status和type返回代拿订单列表
     * @param dnStatus 订单状态（传10返回所有状态的订单）
     * @param dnType 订单类型（传10返回所有类型的订单）
     * @param startPage 起始页
     * @param endPage 终止页
     * @return
     */
    public ResultUtil<Object> listDnService(int dnStatus,int dnType,Integer startPage, Integer endPage){
        ResultUtil<Object> resultUtil = new ResultUtil<>();
        try {

            //查询全部
            if(dnStatus == 10 && dnType == 10){
                List<Dn> dns = dnMapper.listAllDn(startPage,endPage);

                resultUtil.setCode(200);
                resultUtil.setMsg("操作成功");
                resultUtil.setData(dns);
                return resultUtil;
            }
            //根据类型查询
            if(dnStatus == 10){
                List<Dn> dns = dnMapper.listDnByType(dnType,startPage,endPage);

                resultUtil.setCode(200);
                resultUtil.setMsg("操作成功");
                resultUtil.setData(dns);
                return resultUtil;
            }
            //根据状态查询
            if(dnType == 10){
                List<Dn> dns = dnMapper.listDnByStatus(dnStatus,startPage,endPage);

                resultUtil.setCode(200);
                resultUtil.setMsg("操作成功");
                resultUtil.setData(dns);
                return resultUtil;
            }
            List<Dn> dns = dnMapper.listDn(dnStatus,dnType,startPage,endPage);

            resultUtil.setCode(200);
            resultUtil.setMsg("操作成功");
            resultUtil.setData(dns);
            return resultUtil;
        }catch (Exception e){
            resultUtil.setCode(100);
            resultUtil.setMsg("操作失败");
            resultUtil.setData(e);
            return resultUtil;
        }
    }

    /**
     * 根据用户uuid查找订单
     * @param userUuid 用户uuid
     * @return
     */
    public ResultUtil<Object> listUserDnService(String userUuid,Integer startPage,Integer endPage){
        ResultUtil<Object> resultUtil = new ResultUtil<>();
        try {
            List<Dn> dns = dnMapper.listUserDn(userUuid,startPage,endPage);

            resultUtil.setCode(200);
            resultUtil.setMsg("操作成功");
            resultUtil.setData(dns);
            return resultUtil;
        }catch (Exception e){
            resultUtil.setCode(100);
            resultUtil.setMsg("操作失败");
            resultUtil.setData(e);
            return resultUtil;
        }
    }

    /**
     * 根据猎人uuid查找订单
     * @param hunterUuid 猎人uuid
     * @return
     */
    public ResultUtil<Object> listHunterDnService(String hunterUuid,Integer startPage,Integer endPage){
        ResultUtil<Object> resultUtil = new ResultUtil<>();
        try {
            List<Dn> dns = dnMapper.listHunterDn(hunterUuid,startPage,endPage);

            resultUtil.setCode(200);
            resultUtil.setMsg("操作成功");
            resultUtil.setData(dns);
            return resultUtil;
        }catch (Exception e){
            resultUtil.setCode(100);
            resultUtil.setMsg("操作失败");
            resultUtil.setData(e);
            return resultUtil;
        }
    }

    /**
     * 猎人接受代拿订单
     * @param dnUuid 代拿订单uuid
     * @return
     */
    public ResultUtil<Object> acceptDnService(String dnUuid,String hunterUuid){
        ResultUtil<Object> resultUtil = new ResultUtil<>();
        try {
            int res = dnMapper.acceptDn(dnUuid,hunterUuid);

            resultUtil.setCode(200);
            resultUtil.setMsg("操作成功");
            resultUtil.setData(res);
            return resultUtil;
        }catch (Exception e){
            resultUtil.setCode(100);
            resultUtil.setMsg("操作失败");
            resultUtil.setData(e);
            return resultUtil;
        }
    }

    /**
     * 代拿订单完成
     * @param dnUuid 订单uuid
     * @param hunterScore 给猎人评分
     * @return
     */
    public ResultUtil<Object> finishDnService(String dnUuid,int hunterScore){
        ResultUtil<Object> resultUtil = new ResultUtil<>();
        try {
            //获取订单详情
            Dn dn = dnMapper.getDn(dnUuid);

            //开启事务
            try{
                //猎人余额增加
                int i1 = hunterMapper.hunterWalletAdd(dn.getDnHunterUuid(),dn.getDnCost());
                //猎人订单总数增加
                int i2 = hunterMapper.hunterDnNumAdd(dn.getDnHunterUuid());
                //猎人个人评分增加
                int i3 = hunterMapper.hunterTotalScoreAdd(dn.getDnHunterUuid(),hunterScore);

                //完成订单
                int i4 = dnMapper.finishDn(dnUuid);

                if(i1 == 0 || i2 == 0 || i3 == 0 || i4 == 0){
                    //设置手动回滚
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                }

            } catch (Exception e) {
                e.printStackTrace();
                //设置手动回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

                resultUtil.setCode(100);
                resultUtil.setMsg("操作失败");
                resultUtil.setData(e);
                return resultUtil;
            }

            resultUtil.setCode(200);
            resultUtil.setMsg("订单完成");
            resultUtil.setData("");
            return resultUtil;
        }catch (Exception e){
            resultUtil.setCode(100);
            resultUtil.setMsg("操作失败");
            resultUtil.setData(e);
            return resultUtil;
        }
    }

    /**
     * 获取订单详细信息
     * @param dnUuid 订单uuid
     * @return
     */
    public ResultUtil<Object> getDnService(String dnUuid){
        ResultUtil<Object> resultUtil = new ResultUtil<>();
        try {
            Dn dn = dnMapper.getDn(dnUuid);

            resultUtil.setCode(200);
            resultUtil.setMsg("操作成功");
            resultUtil.setData(dn);
            return resultUtil;
        }catch (Exception e){
            resultUtil.setCode(100);
            resultUtil.setMsg("操作失败");
            resultUtil.setData(e);
            return resultUtil;
        }
    }
}
